using System.Windows;
using System.Windows.Media.Imaging;

namespace LicenseApp.Client;

/// <summary>
/// 非阻塞对话框
/// </summary>
public partial class NonBlockingDialog : Window
{
    public NonBlockingDialog(string title, string message, LicenseDialogType type = LicenseDialogType.Information, Window? owner = null)
    {
        InitializeComponent();
        
        // 设置父窗口
        if (owner != null)
        {
            Owner = owner;
        }
        else
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }

        // 设置内容
        Title = title;
        titleText.Text = title;
        messageText.Text = message;

        // 设置图标和样式
        SetDialogStyle(type);

        // 自动关闭定时器（可选）
        SetAutoCloseTimer(type);
    }

    private void SetDialogStyle(LicenseDialogType type)
    {
        try
        {
            switch (type)
            {
                case LicenseDialogType.Information:
                    iconImage.Source = new BitmapImage(new Uri("pack://application:,,,/System.Drawing;component/Information.ico"));
                    btnClose.Visibility = Visibility.Collapsed;
                    break;

                case LicenseDialogType.Warning:
                    iconImage.Source = new BitmapImage(new Uri("pack://application:,,,/System.Drawing;component/Warning.ico"));
                    titleText.Foreground = System.Windows.Media.Brushes.Orange;
                    break;

                case LicenseDialogType.Error:
                    iconImage.Source = new BitmapImage(new Uri("pack://application:,,,/System.Drawing;component/Error.ico"));
                    titleText.Foreground = System.Windows.Media.Brushes.Red;
                    break;
            }
        }
        catch
        {
            // 如果图标加载失败，使用文本标识
            iconImage.Visibility = Visibility.Collapsed;
            
            string prefix = type switch
            {
                LicenseDialogType.Warning => "[警告] ",
                LicenseDialogType.Error => "[错误] ",
                _ => "[信息] "
            };
            
            titleText.Text = prefix + titleText.Text;
        }
    }

    private void SetAutoCloseTimer(LicenseDialogType type)
    {
        // 信息类对话框10秒后自动关闭
        if (type == LicenseDialogType.Information)
        {
            var timer = new System.Windows.Threading.DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(10)
            };
            
            timer.Tick += (s, e) =>
            {
                timer.Stop();
                Close();
            };
            
            timer.Start();
        }
    }

    private void BtnOK_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }

    private void BtnClose_Click(object sender, RoutedEventArgs e)
    {
        Close();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        
        // 确保窗口显示在最前面
        Topmost = true;
        Activate();
        Focus();
    }
}
